import sys
import os


def read_data(infile, outfile, begin, size):
    start = int(begin)
    length = int(size)

    infd = open(infile, 'rb')
    infd.seek(start)
    data = infd.read(length)

    outfd = open(outfile, 'wb')
    '''
    before_sig = infd.read(1104)
    outfd.write(before_sig);

    pad_for_align = bytes(4)
    outfd.write(pad_for_align)

    signature_data = infd.read(length-1104)
    outfd.write(signature_data)

    zero = bytes(1)
    remain = (length+4) % 8
    if remain:
        for x in range(0,remain):
            outfd.write(zero)
    '''
    outfd.write(data)
    deadbeef = 'efbeaddeefbeadde'
    append = bytes.fromhex(deadbeef)
    outfd.write(append)
    outfd.write(append)
    outfd.write(append)

    infd.close()
    outfd.close()


if __name__ == '__main__':
    if len(sys.argv) == 5:
        read_data(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
    else:
        print('Usage: %s infile outfile begin size' % sys.argv[0], sys.argv[1], \
            sys.argv[2], sys.argv[3], sys.argv[4])
